package com.qidian.mes.pro.service.impl;

import com.qidian.mes.pojo.Compose;
import com.qidian.mes.pojo.Process;
import com.qidian.mes.pro.mapper.ComposeMapper;
import com.qidian.mes.pro.mapper.ProcessMapper;
import com.qidian.mes.pro.service.ComposeService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author dqy
 * @Description
 * @Time 2024/4/13 9:27
 */
@Service
public class ComposeServiceImpl implements ComposeService {
    @Autowired
    private ComposeMapper composeMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Override
    public QdResult saveCompose(Compose compose) {
        Process process = processMapper.selectByName(compose.getProcessName());
        compose.setProcessId(process.getProcessId());
        compose.setProcessCode(process.getProcessCode());
        return QdResult.isSuccess(composeMapper.insertCompose(compose));
    }

    @Override
    public QdResult queryById(Long composeId) {
        return QdResult.success(composeMapper.selectById(composeId));
    }

    @Override
    public QdResult updateCompose(Compose compose) {
        if (compose.getProcessId()==null)
            return QdResult.error("没有对应的ID");
        System.out.println("得到的compose为"+compose);
        return QdResult.isSuccess(composeMapper.updateCompose(compose));
    }

    @Override
    public QdResult deleteById(Long composeId) {
        composeMapper.deleteCompose(composeId);
        return QdResult.success();
    }

    @Override
    public QdResult deletes(Long[] composeIds) {
        composeMapper.deleteComposes(composeIds);
        return QdResult.success();
    }

    @Override
    public QdResult<PagerDTO<Compose>> queryByPage(Compose compose, int start, int limit) {
        PagerDTO<Compose> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(start);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(composeMapper.selectCount(compose));
        List<Compose> composes = composeMapper.queryByPage(compose, (start-1)*limit, limit);
        System.out.println("查询出来的工艺组成为"+composes);
        pagerDTO.setData(composes);
        return QdResult.success(pagerDTO);
    }
}
